package com.glb.gilibuy.web.mall;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glb.gilibuy.entity.Cart;
import com.glb.gilibuy.entity.Product;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.web.BaseController;
import com.glb.gilibuy.web.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/6 9:21
 */
@Api(tags = "购物车模块")
@Controller
@RequestMapping("/user")
public class CartController extends BaseController {
    @ApiOperation(value = "添加到购物车", notes = "添加到购物车", produces = "application/json")
    @PostMapping("/addCart")
    @ResponseBody
    public Map<String, Object> addCart(@RequestParam("productId") String productId,
                                       @RequestParam(value = "num", defaultValue = "1") Integer num,
                                       HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        Cart cart = new Cart();
        cart.setUserId(userTemp.getUserId());
        cart.setProductId(productId);
        cart.setAmount(num);

        return cartService.addCart(cart);
    }

    @ApiOperation(value = "获取购物车列表", notes = "获取购物车列表", produces = "application/json")
    @GetMapping("/cartList")
    @ResponseBody
    public Map<String, Object> getCartList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                           HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");
        //每页6个
        PageHelper.startPage(page, 6);
        PageInfo pageInfo = cartService.findCartsByUserId(userTemp.getUserId());
        if (pageInfo != null) {
            return MapGenerator.getResWithData(200, "获取购物车列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }
    }

    @ApiOperation(value = "修改购物车项数量", notes = "修改购物车项数量", produces = "application/json")
    @PostMapping("/updateCartNum")
    @ResponseBody
    public Map<String, Object> updateCartNum(@RequestParam("cartId") String cartId,
                                             @RequestParam(value = "num", defaultValue = "1") Integer num,
                                             HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        Cart cart = new Cart();
        cart.setUserId(userTemp.getUserId());
        cart.setCartId(cartId);
        cart.setAmount(num);

        return cartService.updateCart(cart);
    }

    @ApiOperation(value = "删除购物车项", notes = "删除购物车项", produces = "application/json")
    @PostMapping("/deleteCart")
    @ResponseBody
    public Map<String, Object> deleteCart(@RequestParam("cartId") String cartId,
                                          HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        Cart cart = new Cart();
        cart.setUserId(userTemp.getUserId());
        cart.setCartId(cartId);

        return cartService.deleteCart(cart);
    }

    @ApiOperation(value = "获得购物车项总数", notes = "获得购物车项总数", produces = "application/json")
    @GetMapping("/cartCount")
    @ResponseBody
    public Map<String, Object> cartCount(HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");
        Integer count = cartService.selectCountByUserId(userTemp.getUserId());
        return MapGenerator.getResWithData(200, "获取购物车项总数成功！", count);
    }

    @ApiOperation(value = "结算购物车", notes = "结算购物车，并获得结算页面", produces = "application/json")
    @PostMapping("/buy")
    @ResponseBody
    public Map<String, Object> buy(@RequestParam("cartIds") String[] cartIds,
                                   HttpSession httpSession) {
        return cartService.findSelectedCarts(cartIds, httpSession);
    }

    @ApiOperation(value = "直接购买的结算", notes = "直接购买，获得结算页面", produces = "application/json")
    @PostMapping("/buyOne")
    @ResponseBody
    public Map<String, Object> buyOne(@RequestParam("productId") String productId,
                                      @RequestParam(value = "num", defaultValue = "1") Integer num,
                                      HttpSession httpSession) {
        Product product = productService.selectProductByProductId(productId);
        if (product == null) {
            return MapGenerator.getRes(500, "商品不存在！");
        }
        if (num > product.getProductAmount()) {
            return MapGenerator.getRes(501, "选择数量超出商品库存！");
        }
        return cartService.findSelectedProduct(product, num, httpSession);
    }
}
